<template>
<view class="page__bd">
	<custom-navbar bgColor="bg-white" :isBack="true" :isHome="true">
		<block slot="backText">返回</block>
		<block slot="content">商品列表</block>
	</custom-navbar>
	<HMfilterDropdown :menuTop="CustomBar" :filterData="filterData" :defaultSelected="defaultSelected" :updateMenuName="true" @confirm="confirm" dataFormat="Object"></HMfilterDropdown>
	<!-- 占位 -->
	<view class="place" style="height: 76rpx;"></view>
	<view class="daui-card daui-card__full daui-card_access daui-card_one">
		<view class="daui-card__bd">
			<block v-for="(item,index) in productList" :key="index">
				<view class="daui-media daui-media_appmsg daui-media_product">
					<view class="daui-media__hd">
						<image :src="item.logo_show" mode="scaleToFill" class="daui-media__thumb"></image>
					</view>
					<view class="daui-media__bd">
						<view class="daui-media__title" @tap="openProductDetail(item.pid)">{{item.name}}</view>
						<view class="daui-media__desc">{{item.subtitle}}</view>
						<view class="daui-media__tag" v-if="item.attrtag_num > 0">
							<view v-for="(itemTag, indexTag) in item.attrtag" class="daui-badge daui-badge-sm" :class="itemTag.color">
								{{itemTag.name}}
							</view>
						</view>
						<view class="flex justify-between align-end margin-top-xs">
							<view class="flex align-center flex-sub">
								<text class="text-price text-red text-xl">{{item.real_price}}</text>
							</view>
							<view class="padding-left-xl text-right" v-if="item.origin_price != '0.00'">
								<text class="text-price text-gray text-del text-lg margin-left-xs">{{item.origin_price}}</text>
							</view>
						</view>
						<view class="flex justify-between align-end">
							<view class="flex align-center flex-sub">
								<view class="text-gray text-sm flex-sub">已售{{item.sale_num}}</view>
							</view>
							<view class="padding-left-xl text-right">
								<button class="daui-btn bg-primary round shadow sm" @tap="openProductDetail(item.pid)"><text class="icon-lightfill"> </text>抢购</button>
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
	</view>
	
	<!--toast提示-->
	<daui-toast ref="toast"></daui-toast>
</view>
</template>

<script>
import danceuiTool from '../../utils/danceuiTool';
import http from "../../server/api-product.js";
export default {
	data() {
		return {
			StatusBar: this.StatusBar,
			CustomBar: this.CustomBar,
			filterData: [],
			defaultSelected:[],
			cid: 0,
			subcid: 0,
			sort: 0,
			list_param:{
				p:1
			},
			productList: [],
			pageIndex: 1,
			loadding: false,
			pullUpOn: true
		};
	},
	computed: {
		style() {
			var StatusBar = this.StatusBar;
			var CustomBar = this.CustomBar;
			var style = `height:${CustomBar}px;padding-top:${StatusBar}px;`;
			return style
		}
	},
	onLoad(options) {
		console.log('onLoad的参数:', options);
		this.cid = options.cid ? options.cid : 0;
		if(this.cid){
			this.list_param = {...this.list_param, cid:this.cid}
		}
		this.getData();
	},
	onPullDownRefresh() {
		console.log('=====上拉刷新refresh');
		this.list_param = {...this.list_param, p:1}
		this.getData();
		setTimeout(() => {
			uni.stopPullDownRefresh()
		}, 200);
	},
	async onReachBottom() {
		console.log('=====拉到底部onReachBottom');
		if (!this.pullUpOn) return;
		this.loadding = true;
		const token = uni.getStorageSync('token');
		let headers = {"Authorization":"Bearer "+token};
		this.list_param = {...this.list_param, p:++this.nowPage}
		let ret = await http.productSearchlist(this.$refs.toast, this.list_param, headers);
		var retData = ret.data;
		console.log('onReachBottom操作,获取到的数据:', retData);
		if(retData.evaluationList){
			this.evaluationList = [...this.evaluationList,...retData.evaluationList];
			this.nowPage = this.nowPage + 1;
		}else{
			this.loadding = false;
			this.pullUpOn = false;
		}
	},
	methods: {
		async getData(){
			const token = uni.getStorageSync('token');
			let headers = {"Authorization":"Bearer "+token};
			let ret = await http.productSearchlist(this.$refs.toast, this.list_param, headers);
			var retData = ret.data;
			console.log('productList页面,获取到的数据:', retData);
			this.filterData = retData.filterData;
			this.productList = retData.productList;
		},
		confirm(e){ //接收菜单结果
			console.log('接收菜单结果confirm,e:', e);
			this.indexArr = e.index;
			this.valueArr = e.value;
			console.log('cid:'+this.cid+';subcid:'+this.subcid+';sort:'+this.srot);
			console.log('分类一级:', this.valueArr[0][0]);
			console.log('分类二级:', this.valueArr[0][1]);
			console.log('排序二级:', this.valueArr[1][0]);
			var isChange = 0;
			if(this.valueArr[0][0] != null && this.valueArr[0][0] != this.cid){
				console.log('菜单00改变')
				this.cid = this.valueArr[0][0];
				isChange = 1;
			}
			if(this.valueArr[0][1] != null &&this.valueArr[0][1] != this.subcid){
				console.log('菜单01改变')
				this.subcid = this.valueArr[0][1];
				isChange = 1;
			}
			if(this.valueArr[1][0] != null &&this.valueArr[1][0] != this.sort){
				console.log('菜单10改变')
				this.sort = this.valueArr[1][0];
				isChange = 1;
			}
			this.list_param = {...this.list_param, cid: this.cid, subcid: this.subcid}
			if(isChange == 1){
				this.getData();
			}
		},
		openSearch: function() {
			uni.navigateTo({
				url: '/pages/product/search'
			});
		},
		openProductDetail: function(pid) {
			uni.navigateTo({
				url: '/pages/product/productDetail?pid='+pid
			});
		}
	}
};
</script>

<style>
</style>
